export default class Watcher {
    constructor(vm, exp, cb) {
        this.vm = vm;
        this.cb = cb;
        this.getter = parsePath(exp);
        this.value = this.get();
    }
    get() {
        window.target = this;
        const vm = this.vm;
        let val = this.getter.call(vm, vm);
        window.target = null;
        return val;
    }
    update() {
        let oldVal = this.value;
        this.value = this.get();
        this.cb.call(this.vm, this.value, oldVal);
    }
}

/* 不清楚为何这么写 */
/**
 * Parse simple path.
 * 把一个形如'data.a.b.c'的字符串路径所表示的值，从真实的data对象中取出来
 * 例如：
 * data = {a:{b:{c:2}}}
 * parsePath('a.b.c')(data)  // 2
 */
const baseReg = /[^\w.$]/;
export function parsePath(path) {
    if (baseReg.test(path)) {
        return;
    }
    const segment = path.split('.');
    return obj => {
        for (let i = 0; i < segment.length; i++) {
            if (!obj) return;
            obj = obj[segment[i]];
        }
        return obj

    }
}


var removeDuplicates = function(nums) {
    nums = nums.filter((val, index, array) => index === array.indexOf(val))
    return nums.length;
};
removeDuplicates([1, 1, 2])
var removeDuplicates = function(nums) {
    return [...new Set(nums)].length;
};
removeDuplicates([1, 1, 2])